package com.whmnx.chitchat.ui.my;

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.DynamicAdapter;
import com.whmnx.chitchat.adapter.FileAdapter;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.FileBean;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.main.MainActivity;
import com.whmnx.chitchat.main.RegisterActivity;
import com.whmnx.chitchat.utils.FileDownUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.CustomLoadingDialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2020/8/20 0020 9:53
 * version: 1.0
 * @author Administrator
 */
public class MyFileActivity extends BaseActivity {
    @BindView(R.id.rl)
    RecyclerView rlFile;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    @BindView(R.id.message_search)
    EditText query;

    private int page = 1;
    private int Rows = 10;
    private  FileAdapter  fileAdapter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.my_file_activity);
        setTitle("我的文件");
    }

    @Override
    protected void loadData() {
        rlFile.setLayoutManager(new LinearLayoutManager(this));
        rlFile.setItemAnimator(new DefaultItemAnimator());
        fileAdapter = new FileAdapter(this);
        rlFile.setAdapter(fileAdapter);
        getForumList(true);
        refresh.setOnRefreshListener(refreshLayout -> getForumList(true));
        refresh.setOnLoadMoreListener(refreshLayout -> getForumList(false));

        query.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //点击搜索的时候隐藏软键盘
                // hideKeyboard(EditText);
                // 在这里写搜索的操作,一般都是网络请求数据
                search(v.getText().toString().trim());
                if (!TextUtils.isEmpty(v.getText().toString().trim())) {
                   // search(v.getText().toString().trim());
                }else {
                  //  UItils.showToastSafe("请输入内容");
                }
                return true;
            }
            return false;
        });
    }

    private void search(String trim) {
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Page", 1 + "");
        params.put("Rows", 50 + "");
        params.put("Con", trim + "");
        HttpClient.get(MyFileActivity.this, Constants.GETFILELIST, params,new CallBack<List<FileBean>>() {
            @Override
            public void onSuccess(List<FileBean> result) {
                if (result.size() == 0){
                    showEmpty(true,vsEmpty,0);
                }else {
                    showEmpty(false, vsEmpty,0);
                }
                fileAdapter.replaceAll(result);
            }
        });
    }

    private void getForumList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        HttpClient.get(MyFileActivity.this, Constants.GETFILELIST, params,new CallBack<List<FileBean>>() {
            @Override
            public void onSuccess(List<FileBean> result) {
                if (isRefresh){
                    refresh.finishRefresh(true);
                    if (result.size() == 0){
                        showEmpty(true,vsEmpty,0);
                    }else {
                        showEmpty(false, vsEmpty,0);
                    }
                    fileAdapter.replaceAll(result, true);
                }else {
                    if (result.size() > 0) {
                        refresh.finishLoadMore(true);
                        fileAdapter.addAll(result);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });

        fileAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                CustomLoadingDialog.show(MyFileActivity.this);

                ProgressDialog   progressDialog = new ProgressDialog(MyFileActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMessage("下载中");
                progressDialog.setTitle("提示");
                progressDialog.setMax(100);
                progressDialog.setIndeterminate(false);

                progressDialog.setCancelable(false);
              //  progressDialog.show();
                FileBean fileBean = fileAdapter.getItem(i);
                String file = fileBean.getChatRecord_Content();
                String[] strings = file.split("##");
                FileDownUtil.downloadAPK(strings[0],strings[1]);
              /*  Map<String, Object> params = new HashMap<>();
                params.put("Url",strings[0]);
                HttpClient.get(MyFileActivity.this, Constants.DOWNLOADFILE, params,new CallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean result) {  //1234567a

                    }
                });*/

             //创建下载任务,downloadUrl就是下载链接
             //   DownloadManager.Request request = new DownloadManager.Request(Uri.parse(strings[0]));
             //指定下载路径和下载文件名
               // request.setDestinationInExternalPublicDir("/download/", strings[1]);
              //获取下载管理器
             //   DownloadManager downloadManager= (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
            //将下载任务加入下载队列，否则不会进行下载
              //  downloadManager.enqueue(request);
            }
        });
    }
}
